import java.lang.*;
import java.util.*;

public class TypeC extends Thread 
{
	private static Semaphore G2, G5, G1, G4, G3;
	private static Storage St;
	private static DataCollection Data;
	private static System currentTime;
	private static Random randomC = new Random(3);

	private static void MoveToG2()
	{
		double g2Time = 1200-150+2*150*randomC.nextFloat(); /* 1.2+-0.15 hour, define 1hour = 1000ms*/
		try
		{
			sleep((long)g2Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	private static void MoveToG5()
	{
		double g5Time = 250-150+2*150*randomC.nextFloat(); /* 0.25+-0.15 hour */
		try
		{
			sleep((long)g5Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	private static void MoveToG1()
	{
		double g1Time = 700-150+2*150*randomC.nextFloat(); /* 0.7+-0.15 hour */
		try
		{
			sleep((long)g1Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	private static void MoveToG4()
	{
		double g4Time = 900-150+2*150*randomC.nextFloat(); /* 0.9+-0.15 hour */
		try
		{
			sleep((long)g4Time);		
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}
	
	private static void MoveToG3()
	{
		double g3Time = 1000-150+2*150*randomC.nextFloat(); /* 1+-0.15 hour */
		try
		{
			sleep((long)g3Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	
	public TypeC(Semaphore group2, Semaphore group5, Semaphore group1, Semaphore group4, Semaphore group3, Storage myStorage, DataCollection d)
	{		
		
		G2 = group2;
		G5 = group5;
		G1 = group1;
		G4 = group4;
		G3 = group3;
		St = myStorage;
		Data = d;
		setDaemon(true);
	}
	
	public void run()
	{
			
		long tBeg, tG20, tG21, tG50, tG51, tG10, tG11, tG40, tG41, tG30, tG31, tStW0, tStW1, tEnd;
		
		tBeg = currentTime.currentTimeMillis(); 
		
		//move to G2
		tG20 = currentTime.currentTimeMillis(); 
		if(G2.getValue()<=0)
		{
			Data.getG2WQ();
		}	   	
		G2.take();    			
		tG21 = currentTime.currentTimeMillis();
		Data.getG2WT(tG21-tG20);//get G2 waiting time	
  		     		
  		MoveToG2();      		
  		G2.release(); 
  		
  		//move to G5
		tG50 = currentTime.currentTimeMillis();
		if(G5.getValue()<=0)
		{
			Data.getG5WQ();
		}	   	
		G5.take();    			
		tG51 = currentTime.currentTimeMillis();
		Data.getG5WT(tG51-tG50);//get G5 waiting time	
  		     		
  		MoveToG5();
  		G5.release();   		  
  		
  		//move to G1
		tG10 = currentTime.currentTimeMillis(); 
		if(G1.getValue()<=0)
		{
			Data.getG1WQ();
		}	   	   			
		G1.take();
		tG11 = currentTime.currentTimeMillis();
		Data.getG1WT(tG11-tG10); //get G1 waiting time
		 		      		
  		MoveToG1();
  		G1.release();  	
  		
  		//move to G4
		tG40 = currentTime.currentTimeMillis(); 
		if(G4.getValue()<=0)
		{
			Data.getG4WQ();
		}	   		
		G4.take();
		tG41 = currentTime.currentTimeMillis();
		Data.getG4WT(tG41-tG40); //get G4 waiting time  		
  		
  		MoveToG4();
  		G4.release(); 	
  	
  		//get intermediate product from Storage
		tStW0 = currentTime.currentTimeMillis();
		if(St.getValue()<=0)
		{
			Data.getStWQ();
		}	
		St.take();    			
		tStW1 = currentTime.currentTimeMillis();
		Data.getStWT(tStW1-tStW0);//get wait G5 waiting time	
  		     		
  		
  		//move to G3
		tG30 = currentTime.currentTimeMillis(); 
		if(G3.getValue()<=0)
		{
			Data.getG3WQ();
		}	   	
		G3.take();    			
		tG31 = currentTime.currentTimeMillis();
		Data.getG3WT(tG31-tG30);//get G3 waiting time	
  		     		
  		MoveToG3();      		
  		G3.release(); 
  		
  	
  		
  		//type3 residence time and throughput
  		tEnd = currentTime.currentTimeMillis();  		
  		Data.getCR(tEnd - tBeg); 
  		
  		//System.out.println("T3 Running");
  		
      	
    }
}
      		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	